package com.xkdx.caipiao.toggle;

import android.content.Context;
import android.view.Gravity;
import android.view.animation.TranslateAnimation;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.ToggleButton;

import com.xkdx.caipiao.R;
import com.xkdx.caipiao.application.MyApplication;
import com.xkdx.caipiao.statics.IConstants;

/**
 * 状态按钮的监听事件
 * 
 * @author zshun
 * 
 */
public class ToggleListener implements OnCheckedChangeListener {
	private Context context;
	private String settingName;
	private ToggleButton toggle;
	private ImageButton toggle_Button;

	public ToggleListener(Context context, String settingName,
			ToggleButton toggle, ImageButton toggle_Button) {
		this.context = context;
		this.settingName = settingName;
		this.toggle = toggle;
		this.toggle_Button = toggle_Button;
	}

	@Override
	public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
		// 保存设置
		if (IConstants.SEVEN_COLOR.equals(settingName)) {
			SettingUtils.set(context, SettingUtils.SEVEN_COLOR, isChecked);
		} else if (IConstants.LOTTO.equals(settingName)) {
			SettingUtils.set(context, SettingUtils.LOTTO, isChecked);
		}else if (IConstants.ARRANGE_3.equals(settingName)) {
			SettingUtils.set(context, SettingUtils.ARRAY_3, isChecked);
		} else if (IConstants.ARRANGE_5.equals(settingName)) {
			SettingUtils.set(context, SettingUtils.ARRAY_5, isChecked);
		}

		// 播放动画
		RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) toggle_Button
				.getLayoutParams();
		if (isChecked) {
			// 调整位置
						params.addRule(RelativeLayout.ALIGN_RIGHT, -1);
						if (IConstants.SEVEN_COLOR.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_LEFT,
									R.id.toggle_seven_color);
							MyApplication.getInstance().isPushSevenColor = true;
						} else if (IConstants.LOTTO.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_lotto);
							MyApplication.getInstance().isPushLottery = true;
						}
						else if (IConstants.ARRANGE_3.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_LEFT,
									R.id.toggle_arrange_3);
							MyApplication.getInstance().isPushThree = true;
						} else if (IConstants.ARRANGE_5.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_LEFT, R.id.toggle_arrange_5);
							MyApplication.getInstance().isPushFive = true;
						}
						
						toggle_Button.setLayoutParams(params);
						toggle_Button.setImageResource(R.drawable.progress_thumb_selector_caipiao);
						toggle.setGravity(Gravity.RIGHT | Gravity.CENTER_VERTICAL);
						// 播放动画
						TranslateAnimation animation = new TranslateAnimation(
								DisplayUtils.dip2px(context, 40), 0, 0, 0);
						animation.setDuration(200);
						toggle_Button.startAnimation(animation);

		} else {
			// 调整位置
						if (IConstants.SEVEN_COLOR.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_RIGHT,
									R.id.toggle_seven_color);
							MyApplication.getInstance().isPushSevenColor = false;
						} else if (IConstants.LOTTO.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_lotto);
							MyApplication.getInstance().isPushLottery = false;
						}else if (IConstants.ARRANGE_3.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_RIGHT,
									R.id.toggle_arrange_3);
							MyApplication.getInstance().isPushThree = false;
						} else if (IConstants.ARRANGE_5.equals(settingName)) {
							params.addRule(RelativeLayout.ALIGN_RIGHT, R.id.toggle_arrange_5);
							MyApplication.getInstance().isPushFive = false;
						}
						params.addRule(RelativeLayout.ALIGN_LEFT, -1);
						toggle_Button.setLayoutParams(params);
						toggle_Button.setImageResource(R.drawable.progress_thumb_selector_caipiao);

						toggle.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
						// 播放动画
						TranslateAnimation animation = new TranslateAnimation(
								DisplayUtils.dip2px(context, -40), 0, 0, 0);
						animation.setDuration(200);
						toggle_Button.startAnimation(animation);
		}
	}

}
